/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.panes;

import com.eng.scc.core.data.Address;
import com.eng.scc.core.data.People;
import com.eng.scc.core.view.ApplicationContext;
import java.sql.Date;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import org.jdesktop.swingx.JXDatePicker;

/**
 *
 * @author Rafael
 */
public class UserPane extends javax.swing.JPanel
{
    private People people;

    /**
     * Creates new form UserPane
     */
    public UserPane()
    {
        initComponents();
        
        initMasks();
    }
    
    private void initMasks()
    {
        try
        {
            tfRG.setFormatterFactory( new DefaultFormatterFactory( new MaskFormatter( "##########" ) ) );
            tfCPF.setFormatterFactory( new DefaultFormatterFactory( new MaskFormatter( "###.###.###-##" ) ) );
            tfPhone1.setFormatterFactory( new DefaultFormatterFactory( new MaskFormatter( "(##) ####-####" ) ) );
            tfPhone2.setFormatterFactory( new DefaultFormatterFactory( new MaskFormatter( "(##) ####-####" ) ) );
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    public void setSource( People p, Address a ) 
    {
        setPeople( p );
        
        addressPane.setAddress( a );
    }
    
    public void setPeople( People people )
    {
        try
        {
            this.people = people;

            tfName.setText( this.people.getName() );
            tfCPF.setText( this.people.getCpf() );
            tfRG.setText( this.people.getRg() );
            
            tfEmail.setText( this.people.getEmail() );
            tfBirthDate.setDate( this.people.getBirthday());
            
            rbMale.setSelected( this.people.getGender() == People.MALE );
            rbFemale.setSelected( this.people.getGender() == People.FEMALE );
            
            tfPhone1.setText( this.people.getFixPhone() );
            tfPhone2.setText( this.people.getMobilePhone() );
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }

    public void validateForm( com.eng.scc.core.util.Error error )
    {
        addressPane.validateForm( error );
        
        if ( tfName.getText().isEmpty() )
        {
            error.add( "Informe o Nome" );
        }
        
        if ( tfCPF.getText().replaceAll( "\\D", "" ).trim().isEmpty() )
        {
            error.add( "Informe o CPF" );
        }
        
        if ( tfBirthDate.getDate() == null )
        {
            //validar se motorista tem mais de 18 anos
            //a data de hoje tem que ser menor que 18 anos
            error.add( "Informe a Data de Nascimento" );
        }
    }
    
    public People getPeople()
    {
        try
        {
            people.setName( tfName.getText() );
            people.setCpf( tfCPF.getText() );
            people.setRg( tfRG.getText() );
            people.setEmail( tfEmail.getText() );
            people.setBirthday( new Date( tfBirthDate.getDate().getTime() ) );
            people.setGender( rbMale.isSelected() ? People.MALE : People.FEMALE );
            
            people.setFixPhone(tfPhone1.getText());
            people.setMobilePhone(tfPhone2.getText());
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
        
        return people;
    }
    
    public Address getAddress()
    {
        return addressPane.getAddress();
    }
    
    private void initComponents()
    {
        java.awt.GridBagConstraints gridBagConstraints;

        buttonGroup = new javax.swing.ButtonGroup();
        addressPane = new com.eng.scc.core.panes.AddressPane();
        lbName = new javax.swing.JLabel();
        tfName = new javax.swing.JTextField();
        lbCPF = new javax.swing.JLabel();
        tfCPF = new com.eng.scc.core.parts.FormattedTextField();
        tfRG = new com.eng.scc.core.parts.FormattedTextField();
        lbRG = new javax.swing.JLabel();
        lbEmail = new javax.swing.JLabel();
        tfEmail = new javax.swing.JTextField();
        lbbirthDate = new javax.swing.JLabel();
        tfBirthDate = new JXDatePicker();
        jLabel1 = new javax.swing.JLabel();
        rbMale = new javax.swing.JRadioButton();
        rbFemale = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tfPhone1 = new com.eng.scc.core.parts.FormattedTextField();
        tfPhone2 = new com.eng.scc.core.parts.FormattedTextField();

        setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(addressPane, gridBagConstraints);

        lbName.setText("Nome:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(lbName, gridBagConstraints);

        tfName.setMinimumSize(null);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfName, gridBagConstraints);

        lbCPF.setText("CPF:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(lbCPF, gridBagConstraints);

        tfCPF.setMinimumSize(new java.awt.Dimension(120, 20));
        tfCPF.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfCPF, gridBagConstraints);

        tfRG.setMinimumSize(new java.awt.Dimension(120, 20));
        tfRG.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfRG, gridBagConstraints);

        lbRG.setText("RG:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(lbRG, gridBagConstraints);

        lbEmail.setText("Email:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(lbEmail, gridBagConstraints);

        tfEmail.setMinimumSize(new java.awt.Dimension(120, 20));
        tfEmail.setPreferredSize(new java.awt.Dimension(200, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfEmail, gridBagConstraints);

        lbbirthDate.setText("Data Nasc.:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(lbbirthDate, gridBagConstraints);

        tfBirthDate.setFormats( "dd/MM/yyyy" );
        tfBirthDate.setMinimumSize(new java.awt.Dimension(120, 20));
        tfBirthDate.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfBirthDate, gridBagConstraints);

        jLabel1.setText("Gênero:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel1, gridBagConstraints);

        rbMale.setText("Masculino");
        rbMale.setMaximumSize(new java.awt.Dimension(120, 20));
        rbMale.setMinimumSize(new java.awt.Dimension(120, 20));
        rbMale.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(rbMale, gridBagConstraints);

        rbFemale.setText("Feminino");
        rbFemale.setMaximumSize(new java.awt.Dimension(120, 20));
        rbFemale.setMinimumSize(new java.awt.Dimension(120, 20));
        rbFemale.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(rbFemale, gridBagConstraints);

        jLabel2.setText("Telefone:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel2, gridBagConstraints);

        jLabel3.setText("Celular:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel3, gridBagConstraints);

        buttonGroup.add( rbFemale );
        buttonGroup.add( rbMale );
        rbMale.setSelected( true );
        
        tfPhone1.setMinimumSize(new java.awt.Dimension(120, 20));
        tfPhone1.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfPhone1, gridBagConstraints);

        tfPhone2.setMinimumSize(new java.awt.Dimension(120, 20));
        tfPhone2.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfPhone2, gridBagConstraints);
    }

    private com.eng.scc.core.panes.AddressPane addressPane;
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel lbCPF;
    private javax.swing.JLabel lbEmail;
    private javax.swing.JLabel lbName;
    private javax.swing.JLabel lbRG;
    private javax.swing.JLabel lbbirthDate;
    private javax.swing.JRadioButton rbFemale;
    private javax.swing.JRadioButton rbMale;
    private JXDatePicker tfBirthDate;
    private com.eng.scc.core.parts.FormattedTextField tfCPF;
    private javax.swing.JTextField tfEmail;
    private javax.swing.JTextField tfName;
    private com.eng.scc.core.parts.FormattedTextField tfPhone1;
    private com.eng.scc.core.parts.FormattedTextField tfPhone2;
    private com.eng.scc.core.parts.FormattedTextField tfRG;
}
